<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

/**
 * 课程绑定学员、导师
 * @author LIYANG
 *
 */
class talent extends admin{
	private $db;
	public $siteid;
	public $admin_userid;
	
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('course_talent_model');//加载课程人才绑定关系模型
		$this->course_db = pc_base::load_model('course_nsr_model');//加载课程信息模型
		$this->talent_db = pc_base::load_model('talent_model');//加载人才库模型
		$this->siteid = get_siteid();
		$this->admin_userid = param::get_cookie('userid');//获取当前登录的管理员ID
		$model_setting=pc_base::load_model('site_setting_model');
		$this->talent_source= $model_setting->getValue("talent_source",$this->siteid);//获取人才来源
		$this->talent_xl= $model_setting->getValue("talent_xl",$this->siteid);//获取人才学历
		$city = $model_setting->getValue("city",$this->siteid);//获取城市
		$new_city = array();
		foreach ($city as $k=>$v){
			$new_city[$v] = $v;
		}
		$this->city = $new_city;
		$province = $model_setting->getValue("talent_province",$this->siteid);//获取人才省份
		$talent_province = array();
		foreach ($province as $v){
			$talent_province[$v] = $v;
		}
		$this->province = $talent_province;
	}
	
	public function init() {
		$course_id = isset($_GET['course_id']) ? intval($_GET['course_id']) : showmessage(L('message_not_exits'),HTTP_REFERER);
		$course_info = $this->course_db->get_one(array('id'=>$course_id));
		$talent_type = isset($_GET['talent_type']) ? intval($_GET['talent_type']) : 2;
		if ($talent_type == 2) {
			$btn_info = "学员";
		} elseif ($talent_type == 7) {
			$btn_info = "导师";
		}
		$show_header = false;
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$sql = "";
		$sql .= "select a.*,a.updated_by as supdated_by,b.id from nsrweb_course_talent as b left join nsrweb_talent as a on a.id = b.talent_id where a.status = 1 and b.type = ".$talent_type;
		
		// 搜索框
		$title = isset($_GET['title']) ? $_GET['title'] : '';
		
		if (isset($_GET['search'])) {
			if ($title) {
				$sql .= " and a.`real_name` LIKE '%$title%'";
			}
			if (isset($_GET['talent_type_key']) && $talent_type == 7) {
				$talent_key = $_GET['talent_type_key'];
				$sql.= " and a.$talent_key = 1";
			}
		}
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		$sql .= " and a.siteid = ".$this->siteid." order by a.id desc";
		$talent_info = $this->talent_db->my_listinfo($sql,$page,10);
		$pages = $this->talent_db->pages;
		include $this->admin_tpl('course_talent');
	}
	
	/**
	 * 添加
	 */
	public function add()
	{
		
		if (isset($_POST['dosubmit'])) {
			//添加学员
			$talent_type = isset($_GET['talent_type']) ? intval($_GET['talent_type']) : 2;
			$result = $this->talent_db->moter_add($_POST['talent'],$talent_type);
			if ($result) {
				$data=array();
				$data['course_id'] = $_POST['course_id'];
				$data['talent_id'] = $result;
				$data['type'] = $talent_type;
				$data['created_by'] =  $_SESSION['userid'];
				$data['updated_by'] =  $_SESSION['userid'];
				
				$data['create_time'] = date("Y-m-d H:i:s",time());
				$data['update_time'] = date("Y-m-d H:i:s",time());
				$res = $this->db->insert($data);
				if ($res) {
					showmessage(L('add_success'), '?m=course_nsr&c=talent&a=init&course_id='.$data['course_id'].'&talent_type='.$data['type'],'','add');
				} else {
					showmessage(L('add_error'), HTTP_REFERER);
				}
			} else {
				showmessage(L('add_student_error'), HTTP_REFERER);
			}
		} else {
			$show_dialog = $show_validator = true;
			$show_header = '';
			$talent_type=isset($_REQUEST['talent_type'])?$_REQUEST['talent_type']:2;
			
			if (! isset($_REQUEST['course_id']) || empty($_REQUEST['course_id'])) {
				showmessage(L('message_not_exits'), HTTP_REFERER);
			}
			$course_id= $_REQUEST['course_id'];
			pc_base::load_sys_class('format', '', 0);
			pc_base::load_sys_class('form', '', 0);
			if ($talent_type == 2) {
				$action_source = "course_nsr_student";
				include $this->admin_tpl('student_add','student');
			} elseif ($talent_type == 7) {
				$action_source = "course_nsr_mentor";
				include $this->admin_tpl('teacher_add','teacher');
			}
			
		}
	}
	
	public function bindView(){
		$course_id= $_REQUEST['course_id'];
		$talent_type=isset($_REQUEST['talent_type']) ? $_REQUEST['talent_type']:2;
		$talent_array=$this->db->select(array("course_id"=>$course_id,"type"=>$talent_type),"talent_id");
		$where=" status=1 and  siteid =" . $this->siteid;
		if ($talent_type == 2) {
			$where .= " and is_student = 1";
		} elseif ($talent_type == 7 && !isset($_GET['search'])) {
			$where .= " and is_mentor = 1";
		}
		if(!empty($talent_array)){
			foreach ($talent_array as $k => $v) {
				$id[] = intval($v['talent_id']);
			}
			$id = implode('\',\'', $id);
			$where.=" and id not in ('$id')";
		}
		// 搜索框
		$keywords = isset($_GET['keywords']) ? $_GET['keywords'] : '';
		
		if (isset($_GET['search'])) {
			if ($keywords) {
				$where .= " and `real_name` LIKE '%$keywords%'";
			}
			if (isset($_GET['talent_type_key'])) {
				$talent_key = $_GET['talent_type_key'];
				$where.= " and $talent_key = 1";
			}
		}
		
		//获取用户管理员真实姓名数据
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->talent_db->listinfo($where,'id DESC',$page,10);
		$pages = $this->talent_db->pages;
		include $this->admin_tpl('talent_select');
	}
	
	
	public function  bindAction(){
		$course_id= $_REQUEST['course_id'];
		$relation=$_REQUEST['relation'];
		$source=explode("|",$relation);
		$data=array();
		$talent_type=isset($_REQUEST['talent_type'])?intval($_REQUEST['talent_type']):2;
		
		foreach($source as $k=>$v){
			
			$data[$k]['course_id']=$course_id;
			$data[$k]['talent_id']=$v;
			$data[$k]['type']=$talent_type;
			$data[$k]['created_by'] =  $_SESSION['userid'];
			$data[$k]['updated_by'] =  $_SESSION['userid'];
			
			$data[$k]['create_time'] = date("Y-m-d H:i:s");
			$data[$k]['update_time'] = date("Y-m-d H:i:s");
		}
		$res=$this->db->multiInsert($data);
		if($res){
			echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
		}else{
			echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
		}
	}
	
	/**
	 * 删除、批量删除
	 */
	public function del()
	{
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->db->delete("id in ('$id')");
		
		
		if ($result) {
			showmessage(L('del_success'), HTTP_REFERER);
		} else {
			showmessage(L('del_error'), HTTP_REFERER);
		}
	}
	
	/**
	 * 课程人才关联关系批量删除
	 */
	public function deleteAll ()
	{
		
		if(isset($_GET['dosubmit'])) {
			$id = implode('\',\'', $_POST['ids']);
			
			$result = $this->db->delete("id in ('$id')");
			if ($result) {
				showmessage(L('del_success'), HTTP_REFERER);
			} else {
				showmessage(L('del_error'), HTTP_REFERER);
			}
		} else {
			showmessage(L('operation_failure'));
		}
	}
}